package com.jackfangqi.zhihuijizhou.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jackfangqi.zhihuijizhou.R;

import java.util.List;

/**
 * Created by Android Studio
 * Project: ZhiHuiJiZhou
 * Author: Jack
 * Email: jackfangqi1314@gmail.com/jackfangqi@163.com
 * Date: 2015/6/23
 */
public class TutorialAdapter extends PagerAdapter {

    private Context context;
    private List<Integer> imgs;
    private View.OnClickListener onClickListener;

    public TutorialAdapter(Context context, List<Integer> imgs) {
        this.context = context;
        this.imgs = imgs;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public int getCount() {
        if (imgs != null) {
            return imgs.size();
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view;
        if (position == imgs.size() - 1) {
            FrameLayout frameLayout = new FrameLayout(context);
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imgs.get(position));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
            );
            imageView.setLayoutParams(lp);
            frameLayout.addView(imageView);

            LinearLayout linearLayout = new LinearLayout(context);
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM | Gravity.CENTER
            );
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setLayoutParams(layoutParams);

            Button login = new Button(context);
            Button createNew = new Button(context);
            Button go = new Button(context);
            login.setText("登陆");
            createNew.setText("注册");
            go.setText("开始体验");

            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );

            login.setId(R.id.login);
            createNew.setId(R.id.create_new);
            go.setId(R.id.go);

            login.setLayoutParams(layoutParams1);
            createNew.setLayoutParams(layoutParams1);
            go.setLayoutParams(layoutParams1);

            login.setOnClickListener(onClickListener);
            createNew.setOnClickListener(onClickListener);
            go.setOnClickListener(onClickListener);

            linearLayout.addView(login);
            linearLayout.addView(createNew);
            linearLayout.addView(go);

            frameLayout.addView(linearLayout);
            view = frameLayout;
        } else {
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(imgs.get(position));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            view = imageView;
        }

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof View)
            container.removeView((View) object);
    }
}
